#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QLabel>
#include <QtGui>
#include "bass.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    player.verCompatibility();
    player.initBASS();

    ui->setupUi(this);
    ui->trackSlider->setValue(0);

    stream = NULL;
    fileNames.clear();
    state = -1;
    currentTrack = 0;
    echoSync = false;

    status = new QLabel();
    ui->statusBar->addWidget(status);
    ui->actionOpen->setShortcut(QKeySequence("CTRL+O"));
    ui->actionExit->setShortcut(QKeySequence("CTRL+E"));

    connect(ui->actionOpen, SIGNAL(triggered()), SLOT(createPlaylist()));
    connect(ui->actionExit, SIGNAL(triggered()), SLOT(close()));
    connect(ui->volumeSlider, SIGNAL(valueChanged(int)), SLOT(setVolume(int)));
    connect(ui->trackSlider, SIGNAL(valueChanged(int)), SLOT(setPosition(int)));
    connect(ui->trackSlider, SIGNAL(valueChanged(int)), SLOT(maxValue()));
    connect(ui->pauseButton, SIGNAL(clicked()), SLOT(on_pauseButton_clicked()));
    connect(ui->balanceSlider, SIGNAL(valueChanged(int)), SLOT(setBalance(int)));
    connect(&timer, SIGNAL(timeout()), this, SLOT(sliderMove()));
    connect(ui->prevButton, SIGNAL(clicked()), SLOT(on_previousButton_clicked()));
    connect(ui->cleanButton, SIGNAL(clicked()), SLOT(on_cleanButton_clicked()));
    connect(ui->lineEdit, SIGNAL(itemDoubleClicked(QListWidgetItem*)), SLOT(changeTrack()));

//  Equalizer
    connect(ui->eq1, SIGNAL(valueChanged(int)), SLOT(eq1S(int)));
    connect(ui->eq2, SIGNAL(valueChanged(int)), SLOT(eq2S(int)));
    connect(ui->eq3, SIGNAL(valueChanged(int)), SLOT(eq3S(int)));
    connect(ui->eq4, SIGNAL(valueChanged(int)), SLOT(eq4S(int)));
    connect(ui->eq5, SIGNAL(valueChanged(int)), SLOT(eq5S(int)));
    connect(ui->eq6, SIGNAL(valueChanged(int)), SLOT(eq6S(int)));
    connect(ui->eq7, SIGNAL(valueChanged(int)), SLOT(eq7S(int)));
    connect(ui->eq8, SIGNAL(valueChanged(int)), SLOT(eq8S(int)));
    connect(ui->eq9, SIGNAL(valueChanged(int)), SLOT(eq9S(int)));
    connect(ui->eq10, SIGNAL(valueChanged(int)), SLOT(eq10S(int)));
    connect(ui->eq11, SIGNAL(valueChanged(int)), SLOT(eq11S(int)));
    connect(ui->eq12, SIGNAL(valueChanged(int)), SLOT(eq12S(int)));
    connect(ui->eq13, SIGNAL(valueChanged(int)), SLOT(eq13S(int)));
    connect(ui->eq14, SIGNAL(valueChanged(int)), SLOT(eq14S(int)));
    connect(ui->eq15, SIGNAL(valueChanged(int)), SLOT(eq15S(int)));
    connect(ui->eq16, SIGNAL(valueChanged(int)), SLOT(eq16S(int)));
    connect(ui->eq17, SIGNAL(valueChanged(int)), SLOT(eq17S(int)));
    connect(ui->eq18, SIGNAL(valueChanged(int)), SLOT(eq18S(int)));

//  Chorus
    connect(ui->chMix, SIGNAL(valueChanged(int)), SLOT(chMixSet(int)));
    connect(ui->chDepth, SIGNAL(valueChanged(int)), SLOT(chDepthSet(int)));
    connect(ui->chFB, SIGNAL(valueChanged(int)), SLOT(chFBSet(int)));
    connect(ui->chFreq, SIGNAL(valueChanged(int)), SLOT(chFreqSet(int)));
    connect(ui->chDelay, SIGNAL(valueChanged(int)), SLOT(chDelaySet(int)));
    connect(ui->chPhase, SIGNAL(currentIndexChanged(int)), SLOT(chPhase(int)));
    connect(ui->chWF, SIGNAL(currentIndexChanged(int)), SLOT(chWFSet(int)));
    connect(ui->chReset, SIGNAL(clicked()), SLOT(chReset()));

//    Echo
    connect(ui->eMix, SIGNAL(valueChanged(int)), SLOT(eMixSet(int)));
    connect(ui->eFB, SIGNAL(valueChanged(int)), SLOT(eFBSet(int)));
    connect(ui->eRightD, SIGNAL(valueChanged(int)), SLOT(eRightDSet(int)));
    connect(ui->eLeftD, SIGNAL(valueChanged(int)), SLOT(eLeftDSet(int)));
    connect(ui->eSwap, SIGNAL(clicked()), SLOT(eSwapSet()));

//    Flanger
    connect(ui->flMix, SIGNAL(valueChanged(int)), SLOT(flMixSet(int)));
    connect(ui->flDepth, SIGNAL(valueChanged(int)), SLOT(flDepthSet(int)));
    connect(ui->flFB, SIGNAL(valueChanged(int)), SLOT(flFBSet(int)));
    connect(ui->flFreq, SIGNAL(valueChanged(int)), SLOT(flFreqSet(int)));
    connect(ui->flDelay, SIGNAL(valueChanged(int)), SLOT(flDelaySet(int)));
    connect(ui->flPhase, SIGNAL(currentIndexChanged(int)), SLOT(flPhase(int)));
    connect(ui->flWF, SIGNAL(currentIndexChanged(int)), SLOT(flWFSet(int)));

//    Reverb
    connect(ui->revMix, SIGNAL(valueChanged(int)), SLOT(revMixSet(int)));
    connect(ui->revGain, SIGNAL(valueChanged(int)), SLOT(revGainSet(int)));
    connect(ui->revTime, SIGNAL(valueChanged(int)), SLOT(revTimeSet(int)));
    connect(ui->revRatio, SIGNAL(valueChanged(int)), SLOT(revRatioSet(int)));

//    Gargle
    connect(ui->garRate, SIGNAL(valueChanged(int)), SLOT(garRateSet(int)));
    connect(ui->garWF, SIGNAL(currentIndexChanged(int)), SLOT(garWFSet(int)));

//    Distortion
    connect(ui->disGain, SIGNAL(valueChanged(int)), SLOT(disGainSet(int)));
    connect(ui->disEdge, SIGNAL(valueChanged(int)), SLOT(disEdgeSet(int)));
    connect(ui->disFreq, SIGNAL(valueChanged(int)), SLOT(disFreqSet(int)));
    connect(ui->disBW, SIGNAL(valueChanged(int)), SLOT(disBWSet(int)));
    connect(ui->disPLC, SIGNAL(valueChanged(int)), SLOT(disPLCSet(int)));

//    SampleRate
    connect(ui->sampRate, SIGNAL(valueChanged(int)), SLOT(sampRateSet(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
    player.freeStream(stream);
    player.stopBASS();
}

void MainWindow::on_playButton_clicked()
{            
    if (fileNames.size() == 0)
    {
        QMessageBox::critical(0, "Error!", "ERROR:\t The file was not selected!");
        status->setText("Error!");
        return;
    }
    else
    {
        if ((state == 1)||(state == 0))
        {
            player.stopStream(stream);
            ui->trackSlider->setValue(0);
            player.playStream(stream, true);
            timer.start(1000);
        }
        else
        {
            if(state == 2)
            {
                player.playStream(stream, false);
                timer.start(1000);
            }
            else
            {
                if (ui->lineEdit != NULL)
                    beginPlaying();
                else
                    QMessageBox::critical(0, "Error!", "ERROR:\t The stream was not created!");
            }
        }
        state = 1;
        status->setText("Now playing: " + parseFileName(fileNames.at(currentTrack)));
    }
}

void MainWindow::on_stopButton_clicked()
{
    if (stream!=NULL)
    {
        int level = ui->volumeSlider->value();

        player.volumeEffect(stream, ui->volumeSlider->value());
        player.stopStream(stream);
        player.setVolume(stream, level);

        timer.stop();
        ui->trackSlider->setValue(0);

        status->setText("Stop");
        state = 0;
    }
}

void MainWindow::beginPlaying()
{
    player.freeStream(stream);
    ui->trackSlider->setValue(0);
    stream = player.createStream(fileNames.at(currentTrack));
    ui->trackSlider->setMaximum(player.getStreamLength(stream));
    player.playStream(stream, true);
    timer.start(1000);
    status->setText("Now playing: " + parseFileName(fileNames.at(currentTrack)));
    state = 1;
}

void MainWindow::on_pauseButton_clicked()
{
    if (stream!=NULL)
    {
        if(state == 1)
        {            
            int level = ui->volumeSlider->value();

            player.volumeEffect(stream, ui->volumeSlider->value());
            player.pauseStream(stream);
            player.setVolume(stream, level);

            timer.stop();

            status->setText("Pause");
            state = 2;
            return;
        }
//        if(state == 2)
//        {
//            status->setText("Now playing: " + fileNames);
//            player.playStream(stream, false);
//            state = 1;
//            return;
//        }
    }
}

void MainWindow::createPlaylist()
{
    currentTrack = 0;
    ui->lineEdit->clear();
    fileNames.clear();
    on_addButton_clicked();
    if (fileNames.size() != 0)
        beginPlaying();
}

void MainWindow::changeTrack()
{
    currentTrack = ui->lineEdit->currentRow();
    beginPlaying();
}

void MainWindow::on_addButton_clicked()
{
    QStringList tmpFileNames;
    tmpFileNames = QFileDialog::getOpenFileNames(
                this,
                tr("Open File"),
                "",
                tr("Music files (*.mp3 *.wav *.ogg);;All files (*.*)")
                );
    if (tmpFileNames.size() == 0)
        return;
    else
    {
        for(int i=0; i<tmpFileNames.size(); i++)
        {
            ui->lineEdit->addItem(parseFileName(tmpFileNames.at(i)));
            //player.addToPlaylist(tmpFileNames.at(i));
            fileNames.push_back(tmpFileNames.at(i));
        }
    }
}

void MainWindow::on_cleanButton_clicked()
{
    ui->lineEdit->clear();
    fileNames.clear();
    player.stopStream(stream);
    player.freeStream(stream);
    stream = NULL;
    timer.stop();
    ui->trackSlider->setValue(0);
    status->setText(" ");
    state = -1;
}

void MainWindow::on_removeButton_clicked()
{
    int tmp = -1;
    tmp = ui->lineEdit->currentRow();
    if (tmp != -1)
    {
        delete ui->lineEdit->takeItem(tmp);
        fileNames.removeAt(tmp);
        if (fileNames.size() == 0)
        {
            on_cleanButton_clicked();
            return;
        }
        if (currentTrack == fileNames.size())
            currentTrack = 0;
        if (currentTrack == tmp)
            beginPlaying();
    }
}

void MainWindow::on_previousButton_clicked()
{
    if (stream)
    {
        if (currentTrack != 0)
            currentTrack --;
        beginPlaying();
    }
}

void MainWindow::on_nextButton_clicked()
{
    if (stream)
    {
        timer.stop();
        currentTrack++;
        if (fileNames.size() == currentTrack)
            currentTrack = 0;
        if (fileNames.size() != 0)
            beginPlaying();
    }
}

void MainWindow::maxValue()
{
    if (ui->trackSlider->value() == ui->trackSlider->maximum())
    {
        on_nextButton_clicked();
    }
}

void MainWindow::setVolume(int position)
{
    player.setVolume(stream, position);
}

void MainWindow::setPosition(int position)
{
    player.streamSetPos(stream, position);
}

void MainWindow::setBalance(int position)
{
    player.setBalance(stream, position);
}

QString MainWindow::parseFileName(QString fname)
{
    QStringList part = fname.split("\\");
    return part.at(part.size()-1);
}

void MainWindow::sliderMove()
{
    QObject::disconnect(ui->trackSlider,SIGNAL(valueChanged(int)), this, SLOT(setPosition(int)));
    ui->trackSlider->setValue(player.streamGetPos(stream));
    connect(ui->trackSlider, SIGNAL(valueChanged(int)), SLOT(setPosition(int)));
}

void MainWindow::on_equalSwitch_clicked()
{
    if (ui->equalSwitch->isChecked())
    {
        player.equalizerInit(stream);

        eq1S(ui->eq1->value());
        eq2S(ui->eq2->value());
        eq3S(ui->eq3->value());
        eq4S(ui->eq4->value());
        eq5S(ui->eq5->value());
        eq6S(ui->eq6->value());
        eq7S(ui->eq7->value());
        eq8S(ui->eq8->value());
        eq9S(ui->eq9->value());
        eq10S(ui->eq10->value());
        eq11S(ui->eq11->value());
        eq12S(ui->eq12->value());
        eq13S(ui->eq13->value());
        eq14S(ui->eq14->value());
        eq15S(ui->eq15->value());
        eq16S(ui->eq16->value());
        eq17S(ui->eq17->value());
        eq18S(ui->eq18->value());
    }
    else
    {
        player.equalizerRemove(stream);
    }
}

void MainWindow::eq1S(int level)
{
    player.equalizerStart(31, level);
}

void MainWindow::eq2S(int level)
{
    player.equalizerStart(63, level);
}

void MainWindow::eq3S(int level)
{
    player.equalizerStart(87, level);
}

void MainWindow::eq4S(int level)
{
    player.equalizerStart(125, level);
}

void MainWindow::eq5S(int level)
{
    player.equalizerStart(175, level);
}

void MainWindow::eq6S(int level)
{
    player.equalizerStart(250, level);
}

void MainWindow::eq7S(int level)
{
    player.equalizerStart(350, level);
}

void MainWindow::eq8S(int level)
{
    player.equalizerStart(500, level);
}

void MainWindow::eq9S(int level)
{
    player.equalizerStart(700, level);
}

void MainWindow::eq10S(int level)
{
    player.equalizerStart(1000, level);
}

void MainWindow::eq11S(int level)
{
    player.equalizerStart(1400, level);
}

void MainWindow::eq12S(int level)
{
    player.equalizerStart(2000, level);
}

void MainWindow::eq13S(int level)
{
    player.equalizerStart(2800, level);
}

void MainWindow::eq14S(int level)
{
    player.equalizerStart(4000, level);
}

void MainWindow::eq15S(int level)
{
    player.equalizerStart(5600, level);
}

void MainWindow::eq16S(int level)
{
    player.equalizerStart(8000, level);
}

void MainWindow::eq17S(int level)
{
    player.equalizerStart(11200, level);
}

void MainWindow::eq18S(int level)
{
    player.equalizerStart(16000, level);
}

void MainWindow::on_resetEQ_clicked()
{
    ui->eq1->setValue(0);
    ui->eq2->setValue(0);
    ui->eq3->setValue(0);
    ui->eq4->setValue(0);
    ui->eq5->setValue(0);
    ui->eq6->setValue(0);
    ui->eq7->setValue(0);
    ui->eq8->setValue(0);
    ui->eq9->setValue(0);
    ui->eq10->setValue(0);
    ui->eq11->setValue(0);
    ui->eq12->setValue(0);
    ui->eq13->setValue(0);
    ui->eq14->setValue(0);
    ui->eq15->setValue(0);
    ui->eq16->setValue(0);
    ui->eq17->setValue(0);
    ui->eq18->setValue(0);
}

//CHORUS

void MainWindow::on_chActive_clicked()
{
    if (ui->chActive->isChecked())
    {
        player.effectsInit(stream, 0);
        float tmp = (float)ui->chFreq->value()/10;
        player.chorusSetParam(
                    (float)ui->chMix->value(),
                    (float)ui->chDepth->value(),
                    (float)ui->chFB->value(),
                    tmp,
                    (float)ui->chDelay->value(),
                    ui->chPhase->currentIndex(),
                    ui->chWF->currentIndex());
    }
    else
    {
        player.removeEffect(stream, 0);
    }
}

void MainWindow::chMixSet(int level)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)level,
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

void MainWindow::chDepthSet(int level)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)level,
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

void MainWindow::chFBSet(int level)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)level,
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

void MainWindow::chFreqSet(int level)
{
    float tmp = (float)level/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

void MainWindow::chDelaySet(int level)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)level,
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

void MainWindow::chPhase(int index)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                index,
                ui->chWF->currentIndex());
}

void MainWindow::chWFSet(int index)
{
    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                index);
}

void MainWindow::chReset()
{
    ui->chMix->setValue(50);
    ui->chDepth->setValue(10);
    ui->chFB->setValue(25);
    ui->chFreq->setValue(11);
    ui->chDelay->setValue(16);
    ui->chPhase->setCurrentIndex(0);
    ui->chWF->setCurrentIndex(0);

    float tmp = (float)ui->chFreq->value()/10;
    player.chorusSetParam(
                (float)ui->chMix->value(),
                (float)ui->chDepth->value(),
                (float)ui->chFB->value(),
                tmp,
                (float)ui->chDelay->value(),
                ui->chPhase->currentIndex(),
                ui->chWF->currentIndex());
}

//CHORUS

//ECHO

void MainWindow::on_eActive_clicked()
{
    if (ui->eActive->isChecked())
    {
        player.effectsInit(stream, 1);
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)ui->eRightD->value(),
                    (float)ui->eLeftD->value(),
                    ui->eSwap->isChecked());
    }
    else
    {
        player.removeEffect(stream, 1);
    }
}

void MainWindow::eMixSet(int level)
{
    player.echoSetParam(
                (float)level,
                (float)ui->eFB->value(),
                (float)ui->eRightD->value(),
                (float)ui->eLeftD->value(),
                ui->eSwap->isChecked());
}

void MainWindow::eFBSet(int level)
{
    player.echoSetParam(
                (float)ui->eMix->value(),
                (float)level,
                (float)ui->eRightD->value(),
                (float)ui->eLeftD->value(),
                ui->eSwap->isChecked());
}

void MainWindow::eRightDSet(int level)
{
    if(echoSync)
    {
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)level,
                    (float)level,
                    ui->eSwap->isChecked());
        ui->eLeftD->setValue(level);
    }
    else
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)level,
                    (float)ui->eLeftD->value(),
                    ui->eSwap->isChecked());
}

void MainWindow::eLeftDSet(int level)
{
    if(echoSync)
    {
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)level,
                    (float)level,
                    ui->eSwap->isChecked());
        ui->eRightD->setValue(level);
    }
    else
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)ui->eRightD->value(),
                    (float)level,
                    ui->eSwap->isChecked());
}

void MainWindow::eSwapSet()
{
    if (ui->eSwap->isChecked())
    {
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)ui->eRightD->value(),
                    (float)ui->eLeftD->value(),
                    true);
    }
    else
    {
        player.echoSetParam(
                    (float)ui->eMix->value(),
                    (float)ui->eFB->value(),
                    (float)ui->eRightD->value(),
                    (float)ui->eLeftD->value(),
                    false);
    }
}

void MainWindow::on_eReset_clicked()
{
    ui->eMix->setValue(50);
    ui->eFB->setValue(50);
    ui->eRightD->setValue(500);
    ui->eLeftD->setValue(500);
    ui->eSwap->setChecked(false);
    ui->eSync2->setChecked(false);

    player.echoSetParam(
                (float)ui->eMix->value(),
                (float)ui->eFB->value(),
                (float)ui->eRightD->value(),
                (float)ui->eLeftD->value(),
                ui->eSwap->isChecked());
}

void MainWindow::on_eSyncLR_clicked()
{
    ui->eRightD->setValue(ui->eLeftD->value());
}

void MainWindow::on_eSyncRL_clicked()
{
    ui->eLeftD->setValue(ui->eRightD->value());
}

void MainWindow::on_eSync2_clicked()
{
    if(ui->eSync2->isChecked())
        echoSync = true;
    else
        echoSync = false;
}

//ECHO

//FLANGER

void MainWindow::on_flActive_clicked()
{
    if (ui->flActive->isChecked())
    {
        player.effectsInit(stream, 2);
        float frequency = (float)ui->flFreq->value()/10;
        float delay = (float)ui->flDelay->value()/10;
        player.flangerSetParam(
                    (float)ui->flMix->value(),
                    (float)ui->flDepth->value(),
                    (float)ui->flFB->value(),
                    frequency,
                    delay,
                    ui->flPhase->currentIndex(),
                    ui->flWF->currentIndex());
    }
    else
    {
        player.removeEffect(stream, 2);
    }
}

void MainWindow::flMixSet(int level)
{
    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)level,
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                ui->flWF->currentIndex());
}

void MainWindow::flDepthSet(int level)
{
    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)level,
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                ui->flWF->currentIndex());
}

void MainWindow::flFBSet(int level)
{
    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)level,
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                ui->flWF->currentIndex());
}

void MainWindow::flFreqSet(int level)
{
    float frequency = (float)level/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                ui->flWF->currentIndex());
}

void MainWindow::flDelaySet(int level)
{
    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)level/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                ui->flWF->currentIndex());
}

void MainWindow::flPhaseSet(int index)
{
    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                index,
                ui->flWF->currentIndex());
}

void MainWindow::flWFSet(int index)
{float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flPhase->currentIndex(),
                index);
}

void MainWindow::on_flReset_clicked()
{
    ui->flMix->setValue(50);
    ui->flDepth->setValue(100);
    ui->flFB->setValue(-50);
    ui->flFreq->setValue(2);
    ui->flDelay->setValue(20);
    ui->flWF->setCurrentIndex(0);
    ui->flPhase->setCurrentIndex(0);

    float frequency = (float)ui->flFreq->value()/10;
    float delay = (float)ui->flDelay->value()/10;
    player.flangerSetParam(
                (float)ui->flMix->value(),
                (float)ui->flDepth->value(),
                (float)ui->flFB->value(),
                frequency,
                delay,
                ui->flWF->currentIndex(),
                ui->flPhase->currentIndex());
}

//FLANGER

//REVERB

void MainWindow::on_reverbActive_clicked()
{
    if (ui->reverbActive->isChecked())
    {
        player.effectsInit(stream, 3);
        float tmp = (float)ui->revRatio->value()/1000;
        player.reverbSetParam(
                    (float)ui->revGain->value(),
                    (float)ui->revMix->value(),
                    (float)ui->revTime->value(),
                    tmp);
    }
    else
    {
        player.removeEffect(stream, 3);
    }
}

void MainWindow::revGainSet(int level)
{
    float tmp = (float)ui->revRatio->value()/1000;
    player.reverbSetParam(
                (float)level,
                (float)ui->revMix->value(),
                (float)ui->revTime->value(),
                tmp);
}

void MainWindow::revMixSet(int level)
{
    float tmp = (float)ui->revRatio->value()/1000;
    player.reverbSetParam(
                (float)ui->revGain->value(),
                (float)level,
                (float)ui->revTime->value(),
                tmp);
}

void MainWindow::revTimeSet(int level)
{
    float tmp = (float)ui->revRatio->value()/1000;
    player.reverbSetParam(
                (float)ui->revGain->value(),
                (float)ui->revMix->value(),
                (float)level,
                tmp);
}

void MainWindow::revRatioSet(int level)
{
    float tmp = (float)level/1000;
    player.reverbSetParam(
                (float)ui->revGain->value(),
                (float)ui->revMix->value(),
                (float)ui->revTime->value(),
                tmp);
}

void MainWindow::on_revReset_clicked()
{
    ui->revGain->setValue(0);
    ui->revMix->setValue(0);
    ui->revTime->setValue(1000);
    ui->revRatio->setValue(1);

    player.reverbSetParam(
                (float)ui->revGain->value(),
                (float)ui->revMix->value(),
                (float)ui->revTime->value(),
                (float)ui->revRatio->value());
}

//REVERB

//GARGLE

void MainWindow::on_garActive_clicked()
{
    if (ui->garActive->isChecked())
    {
        player.effectsInit(stream, 4);
        player.gargleSetParam(
                    ui->garRate->value(),
                    ui->garWF->currentIndex());
    }
    else
    {
        player.removeEffect(stream, 4);
    }
}

void MainWindow::garRateSet(int level)
{
    player.gargleSetParam(
                level,
                ui->garWF->currentIndex());
}

void MainWindow::garWFSet(int index)
{
    player.gargleSetParam(
                ui->garRate->value(),
                index);
}

void MainWindow::on_garReset_clicked()
{
    ui->garRate->setValue(20);
    ui->garWF->setCurrentIndex(0);

    player.gargleSetParam(
                ui->garRate->value(),
                ui->garWF->currentIndex());
}

//GARGLE

//DISTORTION

void MainWindow::on_distActive_clicked()
{
    if (ui->distActive->isChecked())
    {
        player.effectsInit(stream, 5);
        player.distortionSetParam(
                    (float)ui->disGain->value(),
                    (float)ui->disEdge->value(),
                    (float)ui->disFreq->value(),
                    (float)ui->disBW->value(),
                    (float)ui->disPLC->value());
    }
    else
    {
        player.removeEffect(stream, 5);
    }
}

void MainWindow::disGainSet(int level)
{
    player.distortionSetParam(
                (float)level,
                (float)ui->disEdge->value(),
                (float)ui->disFreq->value(),
                (float)ui->disBW->value(),
                (float)ui->disPLC->value());
}

void MainWindow::disEdgeSet(int level)
{
    player.distortionSetParam(
                (float)ui->disGain->value(),
                (float)level,
                (float)ui->disFreq->value(),
                (float)ui->disBW->value(),
                (float)ui->disPLC->value());
}

void MainWindow::disFreqSet(int level)
{
    player.distortionSetParam(
                (float)ui->disGain->value(),
                (float)ui->disEdge->value(),
                (float)level,
                (float)ui->disBW->value(),
                (float)ui->disPLC->value());
}

void MainWindow::disBWSet(int level)
{
    player.distortionSetParam(
                (float)ui->disGain->value(),
                (float)ui->disEdge->value(),
                (float)ui->disFreq->value(),
                (float)level,
                (float)ui->disPLC->value());
}

void MainWindow::disPLCSet(int level)
{
    player.distortionSetParam(
                (float)ui->disGain->value(),
                (float)ui->disEdge->value(),
                (float)ui->disFreq->value(),
                (float)ui->disBW->value(),
                (float)level);
}

void MainWindow::on_distReset_clicked()
{
    ui->disGain->setValue(-18);
    ui->disEdge->setValue(15);
    ui->disFreq->setValue(2400);
    ui->disBW->setValue(2400);
    ui->disPLC->setValue(8000);

    player.distortionSetParam(
                (float)ui->disGain->value(),
                (float)ui->disEdge->value(),
                (float)ui->disFreq->value(),
                (float)ui->disBW->value(),
                (float)ui->disPLC->value());
}

void MainWindow::sampRateSet(int level)
{
    player.sampRateSet(stream, level);
}

void MainWindow::on_TPSreset_clicked()
{
    player.sampRateSet(stream, 0);
}
